
package com.xiaoma.myaudience.util.activity;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask.DataAsyncCallback;
import com.xiaoma.myaudience.util.view.menu.MenuBuilder;
import com.xiaoma.myaudience.util.view.menu.NeteaseMenu;

/**
 * 基础Activity类,提供Activity共用方法. <br/>
 * 所有Activity类都继承本类 <br/>
 * 
 * @author wjying
 */
public class BaseActivity extends Activity implements DataAsyncCallback {

    private NeteaseMenu mMenu;

    private LayoutInflater mInflater;

    private View mBaseContainer;

    private FrameLayout mTitleBar;

    private FrameLayout mContent;
    
    private FrameLayout mExtra;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     // 禁用系统标题栏
        super.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mInflater = LayoutInflater.from(this);
        
        super.onCreate(savedInstanceState);    

        // 创建菜单
        MenuBuilder menu = new MenuBuilder(this);
        if (onCreateOptionsNeteaseMenu(menu)) {
            mMenu = new NeteaseMenu(this, menu);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = mInflater.inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        setContentView(view, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        if (mBaseContainer == null) {
            initBaseView();
        }
        
        mContent.addView(view, params);
        super.setContentView(mBaseContainer);
    }

    /**
     * 设置title <br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param resId
     */
    public void setNetEaseTitle(int resId) {
        setNetEaseTitle(getText(resId));
    }

    /**
     * 设置title<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param title
     */
    public void setNetEaseTitle(CharSequence title) {
        TextView titleTextView = (TextView) mTitleBar.findViewById(R.id.base_title_bar_title);
        titleTextView.setText(title);
    }

    /**
     * 设置title对齐位置<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param gravity
     */
    public void setNetEaseTitleGravity(int gravity) {
        TextView titleTextView = (TextView) mTitleBar.findViewById(R.id.base_title_bar_title);
        titleTextView.setGravity(gravity);
    }

    /**
     * 设置titlebar上的icon<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param resId
     */
    public void setNetEaseTitleIcon(int resId) {
        setNetEaseTitleIcon(getResources().getDrawable(resId));
    }

    /**
     * 设置titlebar上的icon<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param icon
     */
    public void setNetEaseTitleIcon(Drawable icon) {
        ImageView iconImageView = (ImageView) mTitleBar.findViewById(R.id.base_title_bar_icon);
        iconImageView.setImageDrawable(icon);
        iconImageView.setVisibility(icon != null ? View.VISIBLE : View.GONE);
    }
    
    /**
     * 设置titlebar上的background<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param resId
     */
    public void setNetEaseTitleBg(int resId) {
        setNetEaseTitleBg(getResources().getDrawable(resId));
    }

    /**
     * 设置titlebar上的background<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param icon
     */
    public void setNetEaseTitleBg(Drawable bg) {
        mTitleBar.setBackgroundDrawable(bg);
    }

    /**
     * 设置titlebar是否可见<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param visibility
     */
    public void setNetEaseTitleBarVisibility(int visibility) {
        mTitleBar.setVisibility(visibility);
//        View titleFade = findViewById(R.id.base_title_bar_fade);
//        titleFade.setVisibility(visibility);
    }

    /**
     * 设置titlebar上的progressbar是否可见<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param visibility
     */
    public void setNetEaseProgressBarVisibility(int visibility) {
        ProgressBar progressBar = (ProgressBar) mTitleBar.findViewById(R.id.base_title_bar_pb);
        progressBar.setVisibility(visibility);
    }

    /**
     * 设置titlebar上的额外内容<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param layoutResID
     */
    public void setNetEaseTitleBarExtra(int layoutResID) {
        View view = mInflater.inflate(layoutResID, null);
        setNetEaseTitleBarExtra(view);
    }

    /**
     * 设置titlebar上的额外内容<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param view
     */
    public void setNetEaseTitleBarExtra(View view) {
        setNetEaseTitleBarExtra(view, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    }

    /**
     * 设置titlebar上的额外内容<br/>
     * 注意此方法需要在{@link #setContentView(int)}, {@link #setContentView(View)},
     * {@link #setContentView(View,LayoutParams)}后
     * 
     * @param view
     * @param params
     */
    public void setNetEaseTitleBarExtra(View view, LayoutParams params) {
        mExtra.setVisibility(View.VISIBLE);
        mExtra.removeAllViews();
        mExtra.addView(view, params);
    }

    /**
     * 创建基础View<br/>
     */
    private void initBaseView() {
        mBaseContainer = mInflater.inflate(R.layout.base_container_layout, null);
        mTitleBar = (FrameLayout) mBaseContainer.findViewById(R.id.base_title_bar);
        mContent = (FrameLayout) mBaseContainer.findViewById(R.id.base_content);
        mExtra = (FrameLayout) mBaseContainer.findViewById(R.id.base_title_bar_extra);
    }

    /**
     * Activity全屏.
     * 
     * @param b
     */
    public void makeFullScreen(boolean b) {
        if (b) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (willDispatchKeyEvent(event)) {
            return true;
        } else {
            Activity parent = getParent();
            if (parent != null && parent instanceof BaseTabActivity
                    && ((BaseTabActivity) parent).willDispatchKeyEvent(event)) {
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

    /**
     * 判断activity会不会执行此次key事件
     * 目前只用于menu判断
     * 
     * @param event
     * @return
     */
    public boolean willDispatchKeyEvent(KeyEvent event) {
        return isNeteaseMenu() && mMenu.dispatchKeyEvent(event);
    }

    /**
     * 是否有菜单
     * 
     * @return
     */
    public boolean isNeteaseMenu() {
        return mMenu != null;
    }

    /**
     * 覆盖此方法使用自定义菜单.
     * 
     * @param menu
     * @return
     */
    public boolean onCreateOptionsNeteaseMenu(Menu menu) {
        return false;
    }

    public void setTitle(CharSequence title) {

    }

    public void setTitle(int titleId) {
        setTitle(getText(titleId));
    }

    @Override
    public void onCancelled(int what) {
    }

    @Override
    public void onPostExecute(int what, Object o) {
    }

    @Override
    public void onPreExecute(int what) {
    }

    @Override
    public void onProgressUpdate(int what, Object o) {
    }
    
    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void Tips(String message, int duration) {
        Toast.makeText(this, message, duration).show();
    }

    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void Tips(String message) {
    	Tips(message, Toast.LENGTH_SHORT);
    }
    
    public void Tips(int resid) {
        Tips(resid, Toast.LENGTH_SHORT);
    }
    
    public void Tips(int resid, int duration) {
        Toast.makeText(this, resid, duration).show();
    }
    
}
